qui {
	clear all
	foreach lhs in $LHSlist {
		noisily display "Cluster, `lhs'"

		use "$datapath/001_nlsy_child_mother_`lhs'.dta", clear
		local subsample = "$subsample"
		
		local sampleset ""

		if ("`subsample'" == "wagebefore") {
			local sampleset "_wage"
			ren wage_hour_beforebirth wage_mom
			replace wage_mom = . if wage_mom == 0
			keep if wage_mom != .
			reg wage_mom  mother_teen mother_2024 mother_2529 mother_3034 mother_old d_birth_*
			predict wage_mom_r, resid
			sum wage_mom_r
			replace wage_mom_r = (wage_mom_r - r(mean))/r(sd)

			gen afqtwage_mom_r = afqt_mom*wage_mom_r
			gen hrswage_r = hours*wage_mom_r
			gen W = wage_mom_r
			gen W2 = W^2

			if ("`modelname'" == "table5_max_trunc") { 
				use "$datapath/001_nlsy_child_mother_`lhs'_new.dta", clear
				local subsample = "$subsample"
				ren wage_hour_beforebirth_max_tr wage_mom
				keep if wage_mom != .
				reg wage_mom  mother_teen mother_2024 mother_2529 mother_3034 mother_old d_birth_*
				predict wage_mom_r, resid
				sum wage_mom_r
				replace wage_mom_r = (wage_mom_r - r(mean))/r(sd)

				gen afqtwage_mom_r = afqt_mom*wage_mom_r
				gen hrswage_r = hours*wage_mom_r
			}

			if ("`modelname'" == "table5_mean_trunc") { 
				use "$datapath/001_nlsy_child_mother_`lhs'_new.dta", clear
				local subsample = "$subsample"
				ren wage_hour_beforebirth_m_tr wage_mom
				keep if wage_mom != .
				reg wage_mom  mother_teen mother_2024 mother_2529 mother_3034 mother_old d_birth_*
				predict wage_mom_r, resid
				sum wage_mom_r
				replace wage_mom_r = (wage_mom_r - r(mean))/r(sd)

				gen afqtwage_mom_r = afqt_mom*wage_mom_r
				gen hrswage_r = hours*wage_mom_r
			}
		}

		if ("`subsample'" == "notsingle") {
			keep if spouse_atbirth == 1
		}
		if ("`subsample'" == "married") {
			keep if mar_status_birth == 1
		}
		if ("`subsample'" == "unmarried") {
			keep if mar_status_birth == 0
		}
		if ("`subsample'" == "loweduc") {
			gen mom_educated = 0
			replace mom_educated = 1 if col_mom == 1 | col_pl_mom == 1
			drop if mom_educated == 1
			drop mom_educated
		}
		if ("`subsample'" == "loweduc_nonsingle") {
			keep if spouse_atbirth == 1
			gen mom_educated = 0
			replace mom_educated = 1 if col_mom == 1 | col_pl_mom == 1
			drop if mom_educated == 1
			drop mom_educated	
		}

		if ("`subsample'" == "non_mom_income") {
			*egen income_before = rowmean(income_hh_before*)
			*replace income_before = . if income_before == 0
			*keep if income_before != .

			ren wage_hour_beforebirth wage_mom
			gen income_mom_before = wage_mom*hrs_worked_yearbefore
			keep if income_mom_before != .
			
			noisily keep if income_hh_before1_mom != .
			noisily keep if income_hh_before1_mom != .
			
			gen non_mom_income = income_hh_before1_mom - income_mom_before
			noisily drop if non_mom_income < 0
			
			noisily sum non_mom_income income_hh_before1_mom income_mom_before
			noisily replace non_mom_income = 0 if non_mom_income < 0
			*some have non-mom-income really really close to mom income, so setting those to zero
			noisily replace non_mom_income = 0 if non_mom_income < 250
			gen non_mom_income0 = (non_mom_income == 0)

			
			noisily sum non_mom_income income_hh_before1_mom income_mom_before
			gen ln_non_mom_income = log(non_mom_income)
			sum ln_non_mom_income
			replace ln_non_mom_income = r(mean) if non_mom_income0 == 1
			
			local sampleset "_income"
			
		}

		if ("`subsample'" == "hh_income_before") {
			egen income_before = rowmean(income_hh_before*)
			replace income_before = . if income_before == 0
			keep if income_before != .
			gen ln_income_before = log(income_before)
		}


		drop if hours == 2080

		gen hhours = hours
		gen hhours2 = hours*hours

		gen noncog_mom2 = noncog_mom*noncog_mom
		gen afqtnoncog = noncog_mom*afqt_mom


		gen hrsafqt = hhours*afqt_mom
		gen hrsnoncog = hhours*noncog_mom

		gen hrseduc = hhours*high_educ

		gen hhours_int = hhours*indd
		gen afqt_mom_int = afqt_mom*indd
		gen hrsafqt_int = hrsafqt*indd


		*keep if work_before_birth>=`WorkBeforeSubsample'
		replace weeks=53 if weeks>53
		gen mother_age_birth2 = mother_age_birth*mother_age_birth

		gen top_bunch_wks = 0
		replace top_bunch_wks = 1 if weeks == 52

		keep $LHSlist $RHSlist ${CONTROLlist_`lhs'`sampleset'} hhours hrs* high_educ
		order $LHSlist $RHSlist ${CONTROLlist_`lhs'`sampleset'} hhours hrs* high_educ


		cluster $linkage ${CONTROLlist_`lhs'`sampleset'}, measure($metric)  // hierarquical clustering on original controls
		generate X_1 = 1
		forvalues k=2(1)100 {
			cluster generate X_`k' = groups(`k'), ties(more)
		}
		noisily save "$datapath/003_cluster_`subsample'_`lhs'.dta", replace
	}
}
